<template>
  <a-modal 
    :confirm-loading="modalOption.loading" 
    :okText="modalOption.okText" 
    @ok="modalOption.onOk"
    @cancel="modalOption.onCancel" 
    :cancelText="modalOption.cancelText" 
    :width="modalOption.width" 
    :visible="visible"
    :title="modalOption.title"
  >
    <a-spin tip="loading" :spinning="loading">
        <div class="container">
        <a-form-model 
          ref="formModel" 
          :label-col="formOption.labelCol" 
          :wrapper-col="formOption.wrapperCol"
          :layout="formOption.layout" 
          :label-align="formOption.labelAlign" 
          :hide-required-mark="formOption.hideRequiredMark"
          :rules="formOption.rules" 
          :model="formOption.formData"
        >
        <!-- 前处理表单 -->
        <div v-if="tabName === '前处理'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="标准" prop="standard" :rules="[{ required: true, message: '标准不能为空' }]">
                        <a-select 
                            :disabled="viewDetail" 
                            @change="standardChange" 
                            v-model="formOption.formData.standard" 
                            placeholder="请选择标准"
                        >
                            <a-select-option value="国标">国标</a-select-option>
                            <a-select-option value="省标">省标</a-select-option>
                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                            <a-select-option value="国标-北京">国标-北京</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="品名" prop="name" :rules="[{ required: true, message: '品名不能为空' }]">
                        <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-if="formOption.formData.standard" 
                            @change="productNameChange" 
                            :disabled="viewDetail" 
                            v-model="formOption.formData.name" 
                            placeholder="请选择品名"
                        >
                            <a-select-option v-for="item in searchSource.productNameList" :key="String(item.id)" :value="item.name">{{ item.name }}</a-select-option>
                        </a-select>
                        <span v-else>请先选择标准</span>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="前处理批号" prop="batch" :rules="[{ required: true, message: '前处理批号不能为空' }]">
                            <a-input :disabled="viewDetail" v-model="formOption.formData.batch" placeholder="请输入前处理批号" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="饮片产出量" prop="prodNum" :rules="[{ required: true, message: '饮片产出量不能为空' }]">
                            <a-input v-model="formOption.formData.prodNum" placeholder="请输入饮片产出量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="饮片生产结束日期" prop="prodEndDate" :rules="[{ required: true, message: '饮片生产结束日期不能为空' }]">
                            <a-date-picker 
                            
                                v-model="formOption.formData.prodEndDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择饮片生产结束日期" 
                                style="width: 100%" 
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row style="border:1px solid #ccc;padding:10px;border-radius:5px;" v-for="(item,i) in formOption.formData.info" :key="i">
                    <a-col :span="12">
                        <a-form-model-item label="药材来料批号" prop="rawBatch" :rules="[{ required: false, message: '药材来料批号不能为空' }]">
                            <a-input  v-model="item.batch" placeholder="请输入药材来料批号" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="药材领用量" prop="takeNum" :rules="[{ required: false, message: '药材领用量不能为空' }]">
                            <a-input  v-model="item.takeNum" placeholder="请输入药材领用量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="药材领用日期" prop="takeDate" :rules="[{ required: false, message: '药材领用日期不能为空' }]">
                            <a-date-picker 
                                v-model="item.takeDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择药材领用日期" 
                                style="width: 100%"
                                @change="timeChange(i)"
                            />
                        </a-form-model-item>
                    </a-col>
                        <a-col :span="12">
                        <a-form-model-item label="药材报告日期" prop="reportDate" :rules="[{ required: false, message: '药材报告日期不能为空' }]">
                            <a-date-picker 
                            
                                v-model="item.reportDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择药材报告日期" 
                                style="width: 100%"
                                @change="timeChange(i)"
                            />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="药材领用与报告时间逻辑判定" prop="logic">
                            <a-input :disabled="true" v-model="item.logic" placeholder="自动计算" suffix="天"/>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item>
                            <a-button style="margin-left: 10px;" type="primary" @click="addMedicinalMaterial">新增</a-button>
                            <a-button type="danger" @click="deleteMedicinalMaterial(i)" :disabled="formOption.formData.info.length<=1" style="margin-left: 8px;">删除</a-button>
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="饮片产出与提取投料平衡判定" prop="balance">
                            <a-input :disabled="true" v-model="formOption.formData.balance" placeholder="自动计算" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <!-- 辅料信息 -->
            <a-card title="辅料信息" hoverable style="margin-top: 16px;">
                <a-row v-for="(item,i) in formOption.formData.flList" :key="i">
                    <a-col :span="6">
                            <a-form-model-item label="辅料名称" prop="flName">
                            <a-select
                                show-search
                                :filter-option="$filterOption"
                                v-model="item.flName" 
                                placeholder="请选择辅料名称"
                                @change="excipientChange($event,i)"
                            >
                                <a-select-option v-for="item in searchSource.excipientNameList" :key="String(item.id)" :value="item.name">{{ item.name }}</a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="6">
                            <a-form-model-item label="辅料批号" prop="flBatch">
                                <a-input  v-model="item.flBatch" placeholder="请输入辅料批号" />
                            </a-form-model-item>
                    </a-col>
                    <a-col :span="6">
                            <a-form-model-item label="使用量" prop="useNum">
                                <a-input  v-model="item.useNum" placeholder="请输入使用量" />
                            </a-form-model-item>
                    </a-col>
                    <a-col :span="6">
                            <a-form-model-item>
                                <a-button type="primary" @click="addExcipient">新增</a-button>
                                <a-button type="danger" @click="deleteExcipient(i)" :disabled="formOption.formData.flList.length<=1" style="margin-left: 8px;">删除</a-button>
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
        <!-- 提取表单 -->
        <div v-if="tabName === '提取'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="标准" prop="standard" :rules="[{ required: true, message: '标准不能为空' }]">
                        <a-select 
                            :disabled="viewDetail" 
                            @change="standardChange" 
                            v-model="formOption.formData.standard" 
                            placeholder="请选择标准"
                        >
                            <a-select-option value="国标">国标</a-select-option>
                            <a-select-option value="省标">省标</a-select-option>
                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                            <a-select-option value="国标-北京">国标-北京</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="品名" prop="name" :rules="[{ required: true, message: '品名不能为空' }]">
                        <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-if="formOption.formData.standard" 
                            @change="productNameChange" 
                            :disabled="viewDetail" 
                            v-model="formOption.formData.name" 
                            placeholder="请选择品名"
                        >
                            <a-select-option v-for="item in searchSource.productNameList" :key="String(item.id)" :value="item.name">{{ item.name }}</a-select-option>
                        </a-select>
                        <span v-else>请先选择标准</span>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="提取批号" prop="batch" :rules="[{ required: true, message: '提取批号不能为空' }]">
                            <a-input :disabled="viewDetail" v-model="formOption.formData.batch" placeholder="请输入提取批号" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="提取来料饮品批号" prop="rawBatch" :rules="[{ required: true, message: '提取来料饮品批号不能为空' }]">
                            <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-model="formOption.formData.rawBatch"
                            placeholder="请选择批号"
                        >
                            <a-select-option v-for="item in searchSource.rawBatchList" :key="String(item.batch)" :value="item.batch">{{ item.batch }}</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="提取投料量" prop="inputNum" :rules="[{ required: true, message: '提取投料量不能为空' }]">
                            <a-input  v-model="formOption.formData.inputNum" placeholder="请输入提取投料量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="提取领用日期" prop="takeDate" :rules="[{ required: true, message: '提取领用日期不能为空' }]">
                            <a-date-picker 
                                v-model="formOption.formData.takeDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择提取领用日期" 
                                style="width: 100%"
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="收粉浸膏粉产出量" prop="sfProdNum" :rules="[{ required: true, message: '收粉浸膏粉产出量不能为空' }]">
                            <a-input  v-model="formOption.formData.sfProdNum" placeholder="请输入收粉浸膏粉产出量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="过筛浸膏粉产出量" prop="gsProdNum" :rules="[{ required: true, message: '过筛浸膏粉产出量不能为空' }]">
                            <a-input  v-model="formOption.formData.gsProdNum" placeholder="请输入过筛浸膏粉产出量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="混一浸膏粉产出量" prop="hyProdNum" :rules="[{ required: true, message: '混一浸膏粉产出量不能为空' }]">
                            <a-input  v-model="formOption.formData.hyProdNum" placeholder="请输入混一浸膏粉产出量" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="混一结束日期" prop="prodEndDate" :rules="[{ required: true, message: '混一结束日期不能为空' }]">
                            <a-date-picker 
                                
                                v-model="formOption.formData.prodEndDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择混一结束日期" 
                                style="width: 100%" 
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="混一产出与混二投入物料平衡判定" prop="balance">
                            <a-input :disabled="true" v-model="formOption.formData.balance" placeholder="自动计算" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="饮片产出与提取领用时间逻辑判定" prop="logic">
                            <a-input :disabled="true" v-model="formOption.formData.logic" placeholder="自动计算" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
        <!-- 制剂表单 -->
        <div v-if="tabName === '制剂'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="标准" prop="standard" :rules="[{ required: true, message: '标准不能为空' }]">
                        <a-select 
                            :disabled="viewDetail" 
                            @change="standardChange" 
                            v-model="formOption.formData.standard" 
                            placeholder="请选择标准"
                        >
                            <a-select-option value="国标">国标</a-select-option>
                            <a-select-option value="省标">省标</a-select-option>
                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                            <a-select-option value="国标-北京">国标-北京</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="品名" prop="name" :rules="[{ required: true, message: '品名不能为空' }]">
                        <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-if="formOption.formData.standard" 
                            @change="productNameChange" 
                            :disabled="viewDetail" 
                            v-model="formOption.formData.name" 
                            placeholder="请选择品名"
                        >
                            <a-select-option v-for="item in searchSource.productNameList" :key="String(item.id)" :value="item.name">{{ item.name }}</a-select-option>
                        </a-select>
                        <span v-else>请先选择标准</span>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="制剂批号" prop="batch" :rules="[{ required: true, message: '制剂批号不能为空' }]">
                            <a-input :disabled="viewDetail" v-model="formOption.formData.batch" placeholder="请输入制剂批号" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="制剂来料提取批号" prop="rawBatch" :rules="[{ required: true, message: '制剂来料提取批号不能为空' }]">
                            <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-model="formOption.formData.rawBatch"
                            placeholder="请选择批号"
                        >
                            <a-select-option v-for="item in searchSource.rawBatchList" :key="String(item.batch)" :value="item.batch">{{ item.batch }}</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="混二领用量" prop="takeNum" :rules="[{ required: true, message: '混二领用量不能为空' }]">
                            <a-input  v-model="formOption.formData.takeNum" placeholder="请输入混二领用量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="混二领用日期" prop="takeDate" :rules="[{ required: true, message: '混二领用日期不能为空' }]">
                            <a-date-picker 
                                 
                                v-model="formOption.formData.takeDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择混二领用日期" 
                                style="width: 100%"
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="混二产出量" prop="prodNum" :rules="[{ required: true, message: '混二产出量不能为空' }]">
                            <a-input  v-model="formOption.formData.prodNum" placeholder="请输入混二产出量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="颗粒产出量" prop="klProdNum" :rules="[{ required: true, message: '颗粒产出量不能为空' }]">
                            <a-input  v-model="formOption.formData.klProdNum" placeholder="请输入颗粒产出量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="颗粒生产结束日期" prop="prodEndDate" :rules="[{ required: true, message: '颗粒生产结束日期不能为空' }]">
                            <a-date-picker 
                                 
                                v-model="formOption.formData.prodEndDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择颗粒生产结束日期" 
                                style="width: 100%" 
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="分装领用量与制粒产出量物料平衡判定" prop="balance">
                            <a-input :disabled="true" v-model="formOption.formData.balance" placeholder="自动计算" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
        <!-- 包装表单 -->
        <div v-if="tabName === '包装'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="标准" prop="standard" :rules="[{ required: true, message: '标准不能为空' }]">
                        <a-select 
                            :disabled="viewDetail" 
                            @change="standardChange" 
                            v-model="formOption.formData.standard" 
                            placeholder="请选择标准"
                        >
                            <a-select-option value="国标">国标</a-select-option>
                            <a-select-option value="省标">省标</a-select-option>
                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                            <a-select-option value="国标-北京">国标-北京</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="品名" prop="name" :rules="[{ required: true, message: '品名不能为空' }]">
                        <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-if="formOption.formData.standard" 
                            @change="productNameChange" 
                            :disabled="viewDetail" 
                            v-model="formOption.formData.name" 
                            placeholder="请选择品名"
                        >
                            <a-select-option v-for="item in searchSource.productNameList" :key="String(item.id)" :value="item.name+'-'+item.inventoryType">{{ item.name }}-{{ item.inventoryType }}</a-select-option>
                        </a-select>
                        <span v-else>请先选择标准</span>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="包装批号" prop="batch" :rules="[{ required: true, message: '包装批号不能为空' }]">
                            <a-input v-model="formOption.formData.batch" placeholder="请输入批号"></a-input>
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="产品大类" prop="inventoryType" :rules="[{ required: true, message: '产品大类不能为空' }]">
                            <a-select
                                :disabled="true"
                                v-if="formOption.formData.name"
                                v-model="formOption.formData.inventoryType" 
                                placeholder="请选择产品大类"
                            >
                                <a-select-option v-if="formOption.formData.standard == '国标'" value="C60">C60</a-select-option>
                                <a-select-option v-if="formOption.formData.standard == '国标'" value="C70">C70</a-select-option>
                                <a-select-option v-if="formOption.formData.standard == '省标'" value="C80">C80</a-select-option>
                                <a-select-option v-if="formOption.formData.standard == '省标'" value="C90">C90</a-select-option>
                                <a-select-option v-if="formOption.formData.standard == '省标-北京'" value="C81">C81</a-select-option>
                                <a-select-option v-if="formOption.formData.standard == '省标-上海'" value="C82">C82</a-select-option>
                                <a-select-option v-if="formOption.formData.standard == '省标-上海'" value="C92">C92</a-select-option>
                            </a-select>
                            <span v-else>请先选择品名</span>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="分装颗粒来料批号" prop="rawBatch" :rules="[{ required: true, message: '分装颗粒来料批号不能为空' }]">
                            <a-select
                            show-search
                            allow-clear
                            :filter-option="$filterOption"
                            v-model="formOption.formData.rawBatch"
                            placeholder="请选择批号"
                        >
                            <a-select-option v-for="item in searchSource.rawBatchList" :key="String(item.batch)" :value="item.batch">{{ item.batch }}</a-select-option>
                        </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="分装领用日期" prop="takeDate" :rules="[{ required: true, message: '分装领用日期不能为空' }]">
                            <a-date-picker 
                                 
                                v-model="formOption.formData.takeDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择分装领用日期" 
                                style="width: 100%"
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="分装颗粒领用量" prop="takeNum" :rules="[{ required: true, message: '分装颗粒领用量不能为空' }]">
                            <a-input v-model="formOption.formData.takeNum" placeholder="请输入分装颗粒领用量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="颗粒退库量" prop="klBackNum" :rules="[{ required: true, message: '颗粒退库量不能为空' }]">
                            <a-input  v-model="formOption.formData.klBackNum" placeholder="请输入颗粒退库量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="颗粒使用量" prop="klUseNum" :rules="[{ required: false, message: '颗粒使用量不能为空' }]">
                            <a-input :disabled="true" v-model="klUseNum" placeholder="请输入颗粒使用量" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="成品产出量" prop="prodNum" :rules="[{ required: true, message: '成品产出量不能为空' }]">
                            <a-input  v-model="formOption.formData.prodNum" placeholder="请输入成品产出量" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="包装结束日期" prop="prodEndDate" :rules="[{ required: true, message: '包装结束日期不能为空' }]">
                            <a-date-picker 
                                 
                                v-model="formOption.formData.prodEndDate" 
                                valueFormat="YYYY-MM-DD" 
                                placeholder="请选择包装结束日期" 
                                style="width: 100%" 
                            />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="制粒结束时间与分装领用时间逻辑判定" prop="logic">
                            <a-input :disabled="true" v-model="formOption.formData.logic" placeholder="自动计算" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
         </a-form-model>
        </div>
    </a-spin>
  </a-modal>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import nextApi from '@/api/nextApi'
@Component({
  name: 'AddOrEdit'
})
export default class AddOrEdit extends Vue {
    @Prop({ type: Boolean, default: false }) visible!: boolean
    @Prop({ type: Boolean, default: false }) viewDetail!: boolean
    @Prop({ type: Object, default: () => ({}) }) editData!: any
    @Prop({type:String, default:""}) tabName!:string
  loading = false

  // 弹窗配置
  modalOption = {
    loading: false,
    okText: '保存',
    cancelText: '取消',
    width: "90%",
    title: '',
    onOk: this.handleSubmit,
    onCancel: this.handleCancel
  }

     // 表单配置
   formOption:any = {
        labelCol: { span: 8 },
        wrapperCol: { span: 15},
        layout: 'horizontal',
        labelAlign: 'right',
        hideRequiredMark: false,
        formData: {
            medicinalMaterialBatchNo:[{batch:''}],
            flList:[
                {
                    flName:'',flBatch:'',useNum:""
                },
            ],
        },
   }

  // 搜索数据源
  searchSource:any = {
    productNameList: [],
    excipientNameList: [],
    rawBatchList:[],
  }

  //计算属性
  get klUseNum(){
        this.formOption.formData.klUseNum = (this.formOption.formData.takeNum || 0) - (this.formOption.formData.klBackNum || 0)
        return this.formOption.formData.klUseNum
  }

  @Watch('visible')
  onVisibleChange(val: boolean) {
    if (val) {
        this.$nextTick(()=>{
            this.initForm()
        })
    }
  }

     // 初始化表单
   initForm() {
    (this.$refs.formModel as any).resetFields()
    if(this.editData.id) {
        this.modalOption.title = this.tabName+'-编辑'
        this.formOption.formData = this.editData
        this.standardChange(this.formOption.formData.standard)
        if(this.tabName==='前处理'){
            this.formOption.formData.flList = this.formOption.formData.flList.length ? this.formOption.formData.flList:[{flname:'',flBatch:'',useNum:''}]
        } 

    } else {
        this.modalOption.title = this.tabName+'-新增'
        // 根据不同的tab设置不同的默认数据结构
        if (this.tabName === '前处理') {
            this.formOption.formData = {
                info:[{batch:'',takeNum:'',reportDate:'',takeDate:'',logic:''}],
                flList:[
                    {
                        flName:'',flBatch:'',useNum:""
                    },
                ],
            }
        } else {
            // 默认结构
            this.formOption.formData = {
                klUseNum:undefined,
            }
        }
    }
   }
   //查询来料批号
  async getRawBatchList(name:string){
    let mapType = ()=>{
        if(this.tabName=='提取') return '前处理'
        if(this.tabName=='制剂') return '提取'
        if(this.tabName=='包装') return '制剂'
    }
    let params:any = {
        type:mapType(),
        name,
        page:1,
        pageSize:1000
    }
    const res = await nextApi.getMaterialStatisticsList(params)
    this.searchSource.rawBatchList = res.data.records || []
    
  }

  // 标准类型变化
  standardChange(value: string) {
    this.getProductNameList(value)
    this.tabName === '前处理'? this.getExcipientNameList():void 0
  }
  // 获取品名列表
  async getProductNameList(value: string) {
    let typeParams:any = ()=>{
        if(this.tabName === '前处理'){
            return {
                materialType:'饮片',
                type:'半成品'
            }
        }else if(this.tabName === '提取'){
            return {
                materialType:'提取物',
                type:'半成品'
            }
        }else if(this.tabName === '制剂'){
            return {
                type:'待包装成品'
            }
        }else if(this.tabName === '包装'){
            return {
                type:'成品'
            }
        }
    }
    let params = typeParams()
    params.standard = value
    params.page = 1
    params.pageSize = 10000
    const res = await nextApi.getMaterialList(params)
    this.searchSource.productNameList = res.data.records || []
  }
  // 获取辅料
  async getExcipientNameList(){
    const res = await nextApi.selectMaterialListByType({materialType:'辅料'})
    this.searchSource.excipientNameList = res.data || []
  }
  // 辅料变化
  excipientChange(v:string,i:number){
    let find = this.searchSource.excipientNameList.find(item=>item.name ===v)
    if(find){
        this.formOption.formData.flList[i].materialStatisticsId = find.id
    }
    console.log(this.formOption.formData);
    
    
  }
  // 品名变化
  productNameChange(value: string) {
    if(this.tabName !== '前处理' && this.tabName !== '制剂'){
        let find = this.searchSource.productNameList.find(item=>item.name===value)
        if(find){
            let name = find.materialName
            this.getRawBatchList(name)
        }
    }
    if(this.tabName == '制剂'){
        let find = this.searchSource.productNameList.find(item=>item.name===value)
        if(find){
            let name = find.materialName.substring(0,find.materialName.length-3)
            this.getRawBatchList(name)
        }
    }
    if(this.tabName=="包装"){
        let find = this.searchSource.productNameList.find(item=>(item.name+'-'+item.inventoryType) ==value)
        if(find){
            this.formOption.formData.inventoryType = find.inventoryType
            this.getRawBatchList(find.name)
        }
    }
  }

  //药材领用/报告日期变化
  timeChange(i:number){
    this.calculateLogicJudgment(i)
  }
  // 新增药材
  addMedicinalMaterial() {
    this.formOption.formData.info.push({batch:'',takeDate:'',reportDate:'',logic:'',takeNum:''})
  }

  // 删除药材
  deleteMedicinalMaterial(i:number) {
    this.formOption.formData.info.splice(i,1)

  }

  // 新增辅料
  addExcipient() {
    this.formOption.formData.flList.push({flName:'',flBatch:'',useNum:""})
  }

  // 删除辅料
  deleteExcipient(i:number) {
    this.formOption.formData.flList.splice(i,1)
  }

     // 计算逻辑判定
   calculateLogicJudgment(i:number) {
        if(!this.formOption.formData.info[i].takeDate || !this.formOption.formData.info[i].reportDate){
            this.formOption.formData.info[i].logic = ''
            return
        }
        const usageDate = new Date(this.formOption.formData.info[i].takeDate)
        const reportDate = new Date(this.formOption.formData.info[i].reportDate)
        if(usageDate > reportDate){
            this.$message.error('药材领用日期不能晚于报告日期')
            this.formOption.formData.info[i].logic = ''
            this.formOption.formData.info[i].reportDate = ''
            return
        }
        const diffTime = Math.abs(usageDate.getTime() - reportDate.getTime())
        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
        if(usageDate && reportDate){
            this.formOption.formData.info[i].logic = diffDays.toString()
        }else{
            this.formOption.formData.info[i].logic = ''
        }
   }

  // 提交表单
    handleSubmit() {
        (this.$refs.formModel as any).validate(async (valid: boolean) =>{
            if(valid){
                try{
                    this.loading = true
                    let params:any = JSON.stringify(this.formOption.formData)
                    params = JSON.parse(params)
                    if(this.tabName==='前处理'){
                        console.log(params.info);
                        
                        if(params.info.every((item:any)=>!item.batch && !item.takeNum && !item.reportDate && !item.logic)){
                            this.$message.error('最少需要一条药材来料信息')
                            return
                        }else{
                            params.reportDate = params.info.map((item:any)=>item.reportDate?item.reportDate:'')
                            params.takeDate = params.info.map((item:any)=>item.takeDate?item.takeDate:'')
                            params.rawBatch = params.info.map((item:any)=>item.batch?item.batch:'')
                            params.takeNum = params.info.map((item:any)=>item.takeNum?item.takeNum:'')

                            params.rawBatch = JSON.stringify(params.rawBatch)
                            params.reportDate = JSON.stringify(params.reportDate)
                            params.takeDate = JSON.stringify(params.takeDate)
                            params.takeNum = JSON.stringify(params.takeNum)
                        }
                        
                    }
                    params.type = this.tabName
                    const res = await nextApi.addMaterialStatistics(params)
                    this.$message.success(res.msg)
                    this.$emit('success')
                    this.handleCancel()
                }finally{
                    this.loading = false
                }
            }
        })
    }


     // 取消
   handleCancel() {
     this.$emit('update:visible', false)
     // 重置表单数据
     if (this.tabName === '前处理') {
            this.formOption.formData = {
                info:[{batch:'',takeDate:'',reportDate:'',logic:'',takeNum:''}],
                flList:[
                    {
                        flName:'',flBatch:'',useNum:""
                    },
                ],
            }
        } else {
            this.formOption.formData = {
                klUseNum:undefined,
            }
        }
   }
}
</script>

<style lang="scss" scoped>
.container {
  .ant-card {
    .ant-card-head {
      background-color: #fafafa;
      border-bottom: 1px solid #d9d9d9;
    }
  }

  .ant-form-item {
    margin-bottom: 16px;
  }

  .ant-row {
    margin-bottom: 8px;
  }
}
</style>
